#include "StdAfx.h"
#include ".\joyoverlappedmanager.h"

CJoyOverlappedManager  s_OverlappedManager;

//##ModelId=44C4B5D200CC
CJoyOverlappedManager::CJoyOverlappedManager(void)
{
	m_iOverlappedCount=0;
}

//##ModelId=44C4B5D200CE
CJoyOverlappedManager::~CJoyOverlappedManager(void)
{
}
//##ModelId=44C4B5D200DA
bool CJoyOverlappedManager::AddOverlapped(SOCKET hSocket,
						LPWSAOVERLAPPED pNewOverlapped,
						LPWSABUF   lpNewBUF,
						DWORD   dwNewBuffersCount,
						LPWSAOVERLAPPED pOldOverlapped,
						LPWSABUF   lpOldBUF,
						DWORD   dwOldBuffersCount,
						struct sockaddr * lpFrom,
						LPINT			lpFromlen,
						LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
						)
{
		if(m_iOverlappedCount>=MAX_OVERLAPPED_COUNT)
		{
			return false;
		}else
		{
			m_Lock.Lock();
			m_OverlappedStruct[m_iOverlappedCount].hSocket=hSocket;
			m_OverlappedStruct[m_iOverlappedCount].pNewOverlapped=pNewOverlapped;
			m_OverlappedStruct[m_iOverlappedCount].lpNewBUF=lpNewBUF;
			m_OverlappedStruct[m_iOverlappedCount].dwNewBuffersCount=dwNewBuffersCount;
			m_OverlappedStruct[m_iOverlappedCount].pOldOverlapped=pOldOverlapped;
			m_OverlappedStruct[m_iOverlappedCount].lpOldBUF=lpOldBUF;
			m_OverlappedStruct[m_iOverlappedCount].dwOldBuffersCount=dwOldBuffersCount;
			m_OverlappedStruct[m_iOverlappedCount].lpFrom=lpFrom;
			m_OverlappedStruct[m_iOverlappedCount].lpFromlen=lpFromlen;
			m_OverlappedStruct[m_iOverlappedCount].lpCompletionRoutine=lpCompletionRoutine;
			m_iOverlappedCount++;
			m_Lock.Unlock();
		}
		return true;
}
//##ModelId=44C4B5D200EE
 bool CJoyOverlappedManager::FindOverlapped(LPWSAOVERLAPPED pNewOverlapped,OVERLAPPEDSTRUCT &myOverlapped)
 {
	 int i=0;
	 m_Lock.Lock();
	 for(i=0;i<m_iOverlappedCount;i++)
	 {
		 if(m_OverlappedStruct[i].pNewOverlapped==pNewOverlapped)
		 {
			 myOverlapped.hSocket=m_OverlappedStruct[i].hSocket;
			 myOverlapped.pNewOverlapped=m_OverlappedStruct[i].pNewOverlapped;
			 myOverlapped.lpNewBUF=m_OverlappedStruct[i].lpNewBUF;
			 myOverlapped.dwNewBuffersCount=m_OverlappedStruct[i].dwNewBuffersCount;
			 myOverlapped.pOldOverlapped=m_OverlappedStruct[i].pOldOverlapped;
			 myOverlapped.lpOldBUF=m_OverlappedStruct[i].lpOldBUF;
			 myOverlapped.dwOldBuffersCount=m_OverlappedStruct[i].dwOldBuffersCount;
			 myOverlapped.lpFrom=m_OverlappedStruct[i].lpFrom;
			 myOverlapped.lpFromlen=m_OverlappedStruct[i].lpFromlen;
			 myOverlapped.lpCompletionRoutine=m_OverlappedStruct[i].lpCompletionRoutine;

			 break;
		 }
	 }
	 m_Lock.Unlock();

	 if(i>=m_iOverlappedCount) return false;
	 else  return true;

 }
//##ModelId=44C4B5D200EC
 int CJoyOverlappedManager::FindOverlapped(LPWSAOVERLAPPED pNewOverlapped)
 {
	int i;

	for(i=0;i<m_iOverlappedCount;i++)
	{
		if(m_OverlappedStruct[i].pNewOverlapped==pNewOverlapped)  break;
	}
	if(i>=m_iOverlappedCount) return -1;

	else return i;

 }
//##ModelId=44C4B5D200FC
 int CJoyOverlappedManager::FindOverlapped(SOCKET hSocket)
 {
	int i;

	for(i=0;i<m_iOverlappedCount;i++)
	{
		if(m_OverlappedStruct[i].hSocket==hSocket)  break;
	}
	if(i>=m_iOverlappedCount) return -1;

	else return i;

 }
//##ModelId=44C4B5D200F1
void CJoyOverlappedManager::DeleteOverlapped(SOCKET hSocket)
{
	int index;
	m_Lock.Lock();
	for(int k=0;k<m_iOverlappedCount;k++)
	{
		index=FindOverlapped(hSocket);
		DeleteOverlapped(index);
	}
	m_Lock.Unlock();
	
}
//##ModelId=44C4B5D200FE
void CJoyOverlappedManager::DeleteOverlapped(int iIndex)
{
	if(iIndex>=m_iOverlappedCount&&iIndex<0) return;
    for(int i=iIndex;i<	m_iOverlappedCount;i++)
	{
		delete m_OverlappedStruct[i].pNewOverlapped;
		delete m_OverlappedStruct[i].lpNewBUF->buf;
		delete m_OverlappedStruct[i].lpNewBUF;
		m_OverlappedStruct[i].hSocket=m_OverlappedStruct[i+1].hSocket;
		m_OverlappedStruct[i].pNewOverlapped=m_OverlappedStruct[i+1].pNewOverlapped;
		m_OverlappedStruct[i].lpNewBUF=m_OverlappedStruct[i+1].lpNewBUF;
		m_OverlappedStruct[i].dwNewBuffersCount=m_OverlappedStruct[i+1].dwNewBuffersCount;
		m_OverlappedStruct[i].pOldOverlapped=m_OverlappedStruct[i+1].pOldOverlapped;
		m_OverlappedStruct[i].lpOldBUF=m_OverlappedStruct[i+1].lpOldBUF;
		m_OverlappedStruct[i].dwOldBuffersCount=m_OverlappedStruct[i+1].dwOldBuffersCount;
		m_OverlappedStruct[i].lpFrom=m_OverlappedStruct[i+1].lpFrom;
		m_OverlappedStruct[i].lpFromlen=m_OverlappedStruct[i+1].lpFromlen;
		m_OverlappedStruct[i].lpCompletionRoutine=m_OverlappedStruct[i+1].lpCompletionRoutine;
		m_iOverlappedCount--;
		

	}

}
//##ModelId=44C4B5D200FB
void CJoyOverlappedManager::Destroy()
{
	m_Lock.Lock();
	for(int i=0;i<m_iOverlappedCount;i++)
	{
		delete m_OverlappedStruct[i].pNewOverlapped;
		delete m_OverlappedStruct[i].lpNewBUF->buf;
		delete m_OverlappedStruct[i].lpNewBUF;
	}
	m_iOverlappedCount=0;
	m_Lock.Unlock();
}
